IOC的核心就是代码入口就在AbstractApplictionContext
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 在刷新容器前进行一些准备工作,例如设置容器的激活状态,校验容器环境所必须的启动参数
prepareRefresh();
// 刷新内部的BeanFactory,获得一个新鲜的BeanFactory,这里面主要是读取XML文件,将其转换为BeanDefinition
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 对BeanFactory进行进一步的完善,包括注册应用上下文感知以及监听器感知的BeanPostProcessor,
// 先注册一些系统的环境Bean
prepareBeanFactory(beanFactory);
try {
// 给子类在初始化BeanFactory重写修改变动beanFactory的权利
postProcessBeanFactory(beanFactory);
// 调用BeanFactoryPostProcessor,可以修改beanFactory,与上面不同的是,这里是类似插件的形式,耦合度更低
invokeBeanFactoryPostProcessors(beanFactory);
// 提前注册BeanPostProcessor,用于后期提供代理等功能
registerBeanPostProcessors(beanFactory);
// 初始化消息源,用于国际化
initMessageSource();
// 初始化应用事件广播器,用于广播应用上下文事件
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// 为应用事件广播器初始化监听器(ApplicationListener)
registerListeners();
// 实例化并注册所有非懒加载的bean
finishBeanFactoryInitialization(beanFactory);
// 刷新容器后的额外工作,初始化生命周期执行器,发布容器刷新完毕的应用上下文事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// 清除掉不再需要的缓存,节省空间
resetCommonCaches();
}
}
}
---分析一下上面代码中的invokeBeanFactoryPostProcessors(beanFactory)源码---开始
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
// 调用所有的BeanFactoryPostProcessors
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
我们知道BeanFactoryPostProcessor可以对已经生成好的BeanDefinition进行修改,也可以动态的添加来自第三方的BeanDefinition。比如我们熟知的Mybatis中的Mapper对象。如果我们使用Spring-Mybatis包中的注解@MapperScan则可以将我们定义的mapper对象加入到Spring容器中,便于我们在项目中注入到指定的地方。
@MapperScan("com.demo")
@Configuration
public class Configuration(){
}
@Service
public class Service{
@Autowired
private DemoMapper demoMapper;
}
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// 存放所有已经调用过的BeanFactoryPostProcessor,避免重复执行
Set<String> processedBeans = new HashSet<String>();
// Spring默认使用的BeanFactory是DefaultListableBeanFactory,其已经实现了BeanDefinitionRegistry接口
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// 用于存放普通的BeanFactoryPostProcessor
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
// 用于存放BeanDefinitionRegistryPostProcessor
List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
new LinkedList<BeanDefinitionRegistryPostProcessor>();
/* 执行我们自定义的beanFactoryPostProcessor,Spring提供customizeContext()方法给予我们定制化构建
ApplicationContext的机会,所以在这个方法中我们可以动态的添加自定义的BeanFactoryPostProcessor。
这里将普通的BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor区分开分别执行。
*/
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryPostProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
registryPostProcessors.add(registryPostProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// 调用实现了PriorityOrdered接口的BeanFactoryPostProcessor
List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
// 按照权重排序
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
registryPostProcessors.addAll(priorityOrderedPostProcessors);
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
// 调用实现了 Ordered接口的,步骤跟上面一样.
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(beanFactory, orderedPostProcessors);
registryPostProcessors.addAll(orderedPostProcessors);
invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
// 最后调用其它的BeanFactoryPostProcessor
// 如果有BeanDefinitionRegistryPostProcessor被执行, 则有可能会产生新的BeanDefinitionRegistryPostProcessor,因此这边将reiterate赋值为true, 代表需要再循环查找一次
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
registryPostProcessors.add(pp);
processedBeans.add(ppName);
pp.postProcessBeanDefinitionRegistry(registry);
reiterate = true;
}
}
}
invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
/* 上面是通过customizeContext来动态添加BeanPostProcessor,而从这里的代码我们可以看出,只要我们写好一个
BeanFactoryPostProcessor,使用任意方法将其加入到Spring容器中,Spring也可以解析后执行。这里足以看出Spring强大的兼容性*/
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// 跳过已经执行过的
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(beanFactory, orderedPostProcessors);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// 清空Bean工厂的元数据缓存,因为在BeanFactoryPostProcessor中可能已经更改了BeanDefinition,所以这里需要清空
beanFactory.clearMetadataCache();
}
这个方法比较长,我个人觉得spring应当将其拆分成一个个小的方法,使结构更加清晰。
BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,它提供了动态注册Bean的能力。
我们最常使用的是它的实现类ConfigurationClassPostProcessor,其带有一个Bean定义读取器。这个读取器可
以读取@Configuration、@Import、@ImportResource注解标注下的所有Bean定义,并最终添加到Spring的BeanFactory中。
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {
***
private ConfigurationClassBeanDefinitionReader reader;
***
}
无论是在普通的Spring项目亦或是SpringBoot项目,只要我们开启了对注解配置的支持,那么系统就会自动注册ConfigurationClassPostProcessor
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
/**
* 获取所有的配置定义,注意:此时系统中没有任何我们自定义的@Configuration配置定义。
* 但如果是SpringBoot项目,系统默认会将启动类SpringBootApplication加入到系统配置定义中去。
*/
Set<BeanDefinitionHolder> configCandidates = new LinkedHashSet<BeanDefinitionHolder>();
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition beanDef = registry.getBeanDefinition(beanName);
if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
}
}
// 如果配置类为空,立即返回
if (configCandidates.isEmpty()) {
return;
}
// 这里初始化一些生成器
SingletonBeanRegistry singletonRegistry = null;
if (registry instanceof SingletonBeanRegistry) {
singletonRegistry = (SingletonBeanRegistry) registry;
if (!this.localBeanNameGeneratorSet && singletonRegistry.containsSingleton(CONFIGURATION_BEAN_NAME_GENERATOR)) {
BeanNameGenerator generator = (BeanNameGenerator) singletonRegistry.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
this.componentScanBeanNameGenerator = generator;
this.importBeanNameGenerator = generator;
}
}
// 递归解析每一个@Configuration配置类
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
for (BeanDefinitionHolder holder : configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
parser.parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
}
else {
parser.parse(bd.getBeanClassName(), holder.getBeanName());
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("Failed to load bean class: " + bd.getBeanClassName(), ex);
}
}
/**
* 校验解析出的所有@Configuration配置类,例如如果是@Configuration注解标注的full配置类,那么类不能被声明为final,否则报错。因为Spring会为
* 每一个@Configuration配置类创建一个CGLIB代理,我们知道CGLIB是通过继承关系来实现代理的,既然用到继承,其父类肯定不能为final。
*/
parser.validate();
// 处理@PropertySource注解的配置类
Stack<PropertySource<?>> parsedPropertySources = parser.getPropertySources();
if (!parsedPropertySources.isEmpty()) {
if (!(this.environment instanceof ConfigurableEnvironment)) {
logger.warn("Ignoring @PropertySource annotations. " +
"Reason: Environment must implement ConfigurableEnvironment");
}
else {
MutablePropertySources envPropertySources = ((ConfigurableEnvironment)this.environment).getPropertySources();
while (!parsedPropertySources.isEmpty()) {
envPropertySources.addLast(parsedPropertySources.pop());
}
}
}
/* 读取每一个@Configuration配置类中定义的bean,加入到ioc容器中。包括使用@Bean注解标注的方法,@Import导入的registry注册器,
* @ImportResource导入的xml资源
*/
if (this.reader == null) {
this.reader = new ConfigurationClassBeanDefinitionReader(
registry, this.sourceExtractor, this.problemReporter, this.metadataReaderFactory,
this.resourceLoader, this.environment, this.importBeanNameGenerator);
}
this.reader.loadBeanDefinitions(parser.getConfigurationClasses());
// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
if (singletonRegistry != null) {
if (!singletonRegistry.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
singletonRegistry.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
}
}
}
---分析一下上面代码中的invokeBeanFactoryPostProcessors(beanFactory)源码---结束
未完待续。。。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。